package com.newtype.meeting.client.mvc.backend;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.GMeeting;
import com.newtype.meeting.client.MD5;
import com.newtype.meeting.client.model.AccountType;
import com.newtype.meeting.client.model.Degree;
import com.newtype.meeting.client.model.Department;
import com.newtype.meeting.client.model.Meeting;
import com.newtype.meeting.client.model.MeetingCategory;
import com.newtype.meeting.client.model.MeetingStart;
import com.newtype.meeting.client.model.Member;
import com.newtype.meeting.client.model.Title;
import com.newtype.meeting.client.service.AccountServiceAsync;
import com.newtype.meeting.client.service.DepartmentServiceAsync;
import com.newtype.meeting.client.service.MeetingCategoryServiceAsync;
import com.newtype.meeting.client.service.MeetingServiceAsync;
import com.newtype.meeting.client.service.MeetingStartServiceAsync;
import com.newtype.meeting.client.service.TitleServiceAsync;
import com.newtype.meeting.shared.AccountDTO;
import com.newtype.meeting.shared.AccountSummaryDTO;
import com.newtype.meeting.shared.DepartmentTreeDTO;

/**
 * 后台管理员操作控制类
 * @author 汪恭泽
 * Jun 7, 2011 1:35:52 PM
 */
public class BackendController extends Controller {

	private AccountServiceAsync accountService;
	private TitleServiceAsync titleService;
	private DepartmentServiceAsync departmentService;
	private MeetingCategoryServiceAsync categoryService;
	private MeetingServiceAsync meetingService;
	private MeetingStartServiceAsync startService;

	private BackendView backendView;

	public BackendController() {
		registerEventTypes(AppEvents.Init);
		registerEventTypes(AppEvents.ViewMenu);
		registerEventTypes(AppEvents.ViewAccountManagement);
		registerEventTypes(AppEvents.ViewDepartmentManagement);
		registerEventTypes(AppEvents.ViewMeetingManagement);
		registerEventTypes(AppEvents.ViewTitleManagement);
		registerEventTypes(AppEvents.SaveTitle);
		registerEventTypes(AppEvents.SaveDepartment);
		registerEventTypes(AppEvents.SaveAccount);
		registerEventTypes(AppEvents.ToDeptTreeSingleWindow);
		registerEventTypes(AppEvents.ToDeptTreeMultiWindow);
		registerEventTypes(AppEvents.ToDeptAddWindow);
		registerEventTypes(AppEvents.ToOpenedNewAccount);
		registerEventTypes(AppEvents.OpenedNewAccount);
		registerEventTypes(AppEvents.DeleteNewAccount);
		registerEventTypes(AppEvents.ToAddNewAccountWindow);
		registerEventTypes(AppEvents.ToTitleEditWindow);
		registerEventTypes(AppEvents.UpdateTitle);
		registerEventTypes(AppEvents.DeleteTitle);
		registerEventTypes(AppEvents.RegisterAccount);
		registerEventTypes(AppEvents.ToMainCategoryWindow);
		registerEventTypes(AppEvents.ToNextCategoryWindow);
		registerEventTypes(AppEvents.SaveorUpdateMainCategory);
		registerEventTypes(AppEvents.SaveorUpdateNextCategory);
		registerEventTypes(AppEvents.CheckAccountIDExist);
		registerEventTypes(AppEvents.DeleteMainCategory);
		registerEventTypes(AppEvents.DeleteNextCategory);
		registerEventTypes(AppEvents.ToVcardEdit);
		registerEventTypes(AppEvents.SaveVcardEdit);
	}

	@Override
	protected void initialize() {
		super.initialize();
		backendView = new BackendView(this);
		accountService = Registry.get(GMeeting.ACCOUNT_SERVICE);
		titleService = Registry.get(GMeeting.TITLE_SERVICE);
		departmentService = Registry.get(GMeeting.DEPARTMENT_SERVICE);
		categoryService = Registry.get(GMeeting.CATEGORY_SERVICE);
		meetingService = Registry.get(GMeeting.MEETING_SERVICE);
		startService = Registry.get(GMeeting.MEETINGSTART_SERVICE);
	}

	@Override
	public void handleEvent(AppEvent event) {
		EventType type = event.getType();
		if (type == AppEvents.ViewAccountManagement) {
			onViewAccountManagement(event);
		} else if (type == AppEvents.ViewDepartmentManagement) {
			onViewDepartmentManagement(event);
		} else if (type == AppEvents.ViewMeetingManagement) {
			onViewMeetingManagement(event);
		} else if (type == AppEvents.ViewTitleManagement) {
			onViewTitleManagement(event);
		} else if (type == AppEvents.SaveTitle) {
			onSaveTitle(event);
		} else if (type == AppEvents.SaveDepartment) {
			onSaveDepartment(event);
		} else if (type == AppEvents.SaveAccount) {
			onSaveAccount(event);
		} else if (type == AppEvents.ToDeptTreeSingleWindow) {
			onToDeptTreeSingleWindow(event);
		} else if (type == AppEvents.ToDeptTreeMultiWindow) {
			onToDeptTreeMultiWindow(event);
		} else if (type == AppEvents.ToDeptAddWindow) {
			onDeptAddWindow(event);
		} else if (type == AppEvents.ToOpenedNewAccount) {
			onToOpenedNewAccount(event);
		} else if (type == AppEvents.OpenedNewAccount) {
			onOpenedNewAccount(event);
		} else if (type == AppEvents.DeleteNewAccount) {
			onDeleteNewAccount(event);
		} else if (type == AppEvents.ToAddNewAccountWindow) {
			onToAddNewAccountWindow(event);
		} else if (type == AppEvents.ToTitleEditWindow) {
			onToTitleEditWindow(event);
		} else if (type == AppEvents.UpdateTitle) {
			onUpdateTitle(event);
		} else if (type == AppEvents.RegisterAccount) {
			onRegisterAccount(event);
		} else if (type == AppEvents.ToMainCategoryWindow) {
			onToMainCategoryWindow(event);
		} else if (type == AppEvents.ToNextCategoryWindow) {
			onToNextCategoryWindow(event);
		} else if (type == AppEvents.SaveorUpdateMainCategory) {
			onSaveOrUpdateMainCategory(event);
		} else if (type == AppEvents.DeleteTitle) {
			onDeleteTilte(event);
		} else if (type == AppEvents.CheckAccountIDExist) {
			onCheckAccountIDExist(event);
		} else if (type == AppEvents.SaveorUpdateNextCategory) {
			onSaveorUpdateNextCategory(event);
		} else if (type == AppEvents.DeleteMainCategory) {
			onDeleteMainCategory(event);
		} else if (type == AppEvents.DeleteNextCategory) {
			onDeleteNextCategory(event);
		} else if (type == AppEvents.ToVcardEdit) {
			onToVcardEdit(event);
		} else if (type == AppEvents.SaveVcardEdit) {
			onSaveVcardEdit(event);
		}
	}

	/**
	 * 保存个人资料修改
	 * @param event
	 */
	private void onSaveVcardEdit(AppEvent event) {
		//接收参数
		final AccountDTO accountDTO = event.getData();
		accountService.updateAccount(accountDTO,
				new AsyncCallback<AccountDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(AccountDTO result) {
						MessageBox.alert("提示信息", "个人资料修改成功", null);
						Dispatcher.forwardEvent(AppEvents.ToVcardEdit,
								accountDTO.getId());
					}
				});
	}

	/**
	 * 跳转到修改我的资料面板
	 * @param event
	 */
	private void onToVcardEdit(AppEvent event) {
		//接收参数
		String id = event.getData();
		final AppEvent ae = new AppEvent(event.getType());
		accountService.getAccount(id,
				new AsyncCallback<AccountDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(AccountDTO result) {
						ae.setData("accountDTO", result);
						forwardToView(backendView, ae);
					}
				});
	}

	/**
	 * 删除会议次类别
	 * @param event
	 */
	private void onDeleteNextCategory(AppEvent event) {
		final MeetingCategory category = event.getData();
		categoryService.deleteCategory(category.getId(),
				new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(Void result) {
						MessageBox.alert("提示信息", category.getCategoryName()
								+ "删除成功", null);
						Dispatcher
								.forwardEvent(AppEvents.ViewMeetingManagement);
					}
				});
	}

	/**
	 * 删除主会议类别
	 * @param event
	 */
	private void onDeleteMainCategory(AppEvent event) {
		final MeetingCategory category = event.getData();
		categoryService.deleteCategory(category.getId(),
				new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(Void result) {
						MessageBox.alert("提示信息", category.getCategoryName()
								+ "删除成功", null);
						Dispatcher
								.forwardEvent(AppEvents.ViewMeetingManagement);
					}
				});
	}

	/**
	 * 保存次会议类别
	 * @param event
	 */
	private void onSaveorUpdateNextCategory(AppEvent event) {
		final MeetingCategory category = event.getData();
		categoryService.saveCategory(category, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(Void result) {
				MessageBox.alert("提示信息", category.getCategoryName() + "保存成功",
						null);
				Dispatcher.forwardEvent(AppEvents.ViewMeetingManagement);
			}
		});
	}

	/**
	 * 检查用户名是否存在
	 * @param event
	 */
	private void onCheckAccountIDExist(AppEvent event) {
		String accountID = event.getData();
		System.out.println("-->" + accountID);
		final AppEvent ae = new AppEvent(event.getType());
		accountService.checkExist(accountID, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				ae.setData("result", result);
				forwardToView(backendView, ae);
			}
		});
	}

	/**
	 * 执行删除职称操作
	 * @param event
	 */
	private void onDeleteTilte(AppEvent event) {
		final Title title = event.getData();
		titleService.deleteTitle(title.getId(), new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(Void result) {
				MessageBox
						.alert("提示信息", title.getTitleCaption() + "删除成功", null);
				Dispatcher.forwardEvent(AppEvents.ViewTitleManagement);
			}
		});
	}

	/**
	 * 保存或更新会议主类别
	 * @param event
	 */
	private void onSaveOrUpdateMainCategory(AppEvent event) {
		final MeetingCategory category = event.getData();
		categoryService.saveCategory(category, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(Void result) {
				MessageBox.alert("提示信息", category.getCategoryName() + "保存成功",
						null);
				Dispatcher.forwardEvent(AppEvents.ViewMeetingManagement);
			}

		});
	}

	/**
	 * 打开次类别新增/维护窗口
	 * @param event
	 */
	private void onToNextCategoryWindow(AppEvent event) {
		final MeetingCategory category = event.getData();
		final AppEvent ae = new AppEvent(event.getType());
		ae.setData("category", category);
		//获取主类别
		categoryService
				.getMainCategories(new AsyncCallback<ArrayList<MeetingCategory>>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(ArrayList<MeetingCategory> result) {
						ae.setData("mainCates", result);
						//判断类别在会议中是否使用
						meetingService.listMeetingByCategory(category.getId(),
								new AsyncCallback<List<Meeting>>() {
									@Override
									public void onFailure(Throwable caught) {
										Dispatcher.forwardEvent(
												AppEvents.Error, caught);
									}

									@Override
									public void onSuccess(List<Meeting> result) {
										boolean f1 = false;
										if (result.size() > 0) {
											f1 = true;
										}
										ae.setData("f1", f1);
										//判断类别在会议中是否使用
										startService
												.listMeetingStartByCategory(
														category.getId(),
														new AsyncCallback<List<MeetingStart>>() {
															@Override
															public void onFailure(
																	Throwable caught) {
																Dispatcher
																		.forwardEvent(
																				AppEvents.Error,
																				caught);
															}

															@Override
															public void onSuccess(
																	List<MeetingStart> result) {
																boolean f2 = false;
																if (result
																		.size() > 0) {
																	f2 = true;
																}
																ae.setData(
																		"f2",
																		f2);
																if (category
																		.getParentId() != null) {
																	//获取父类型对象
																	categoryService
																			.getCategory(
																					category
																							.getParentId(),
																					new AsyncCallback<MeetingCategory>() {
																						@Override
																						public void onFailure(
																								Throwable caught) {
																							Dispatcher
																									.forwardEvent(
																											AppEvents.Error,
																											caught);
																						}

																						@Override
																						public void onSuccess(
																								MeetingCategory result) {
																							MeetingCategory parentCate = new MeetingCategory(
																									result
																											.getId(),
																									result
																											.getCategoryName());
																							ae
																									.setData(
																											"parentCate",
																											parentCate);
																							forwardToView(
																									backendView,
																									ae);
																						}
																					});
																} else {
																	forwardToView(
																			backendView,
																			ae);
																}
															}

														});
									}
								});
					}
				});
	}

	//判断类别是否在会议中使用
	protected boolean getFlag(final MeetingCategory category) {
		final boolean flag = false;

		return flag;
	}

	/**
	 * 打开主类别新增/维护窗口
	 * @param event
	 */
	private void onToMainCategoryWindow(AppEvent event) {
		MeetingCategory category = event.getData();
		final AppEvent ae = new AppEvent(event.getType());
		ae.setData("category", category);
		categoryService.listChildCategory(category,
				new AsyncCallback<List<MeetingCategory>>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(List<MeetingCategory> result) {
						boolean flag = false;
						if (result.size() > 0) {
							flag = true;
						}
						ae.setData("flag", flag);
						forwardToView(backendView, ae);
					}
				});
	}

	/**
	 * 提交帐户申请
	 * @param event
	 */
	private void onRegisterAccount(AppEvent event) {
		final AccountDTO accountDTO = event.getData();
		accountDTO.setPassword(MD5.MD5Encode(accountDTO.getPassword()));//设置密码
		accountDTO.setAccountType(AccountType.USER);//指定用户类型
		accountDTO.setStatus("0");//设置帐号状态
		accountDTO.setRegisterDate(new Date());//设置注册时间

		accountService.updateAccount(accountDTO,
				new AsyncCallback<AccountDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(AccountDTO result) {
						getPromptDialog().show();
					}
				});
	}

	/**
	 * 注册成功后弹出的对话框
	 * @return
	 */
	protected Dialog getPromptDialog() {
		Dialog prompt = new Dialog();
		prompt.setHeading("谢谢您！您的帐号申请资料已经收到！！");
		prompt.setButtons(Dialog.OK);
		prompt.addText("您的申请我们已经收到，将在人事主管核准申请与考核权限后，通知您使用帐号与密码，请等待工作程序时间。");
		prompt.setScrollMode(Scroll.AUTO);
		prompt.setHideOnButtonClick(true);
		prompt.setSize(400, 250);
		prompt.setModal(true);
		return prompt;
	}

	/**
	 * 更新职称信息
	 * @param event
	 */
	private void onUpdateTitle(AppEvent event) {
		final Title title = event.getData();
		titleService.addTitle(title, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(Void result) {
				MessageBox
						.alert("提示信息", title.getTitleCaption() + "更新成功", null);
				Dispatcher.forwardEvent(AppEvents.ViewTitleManagement);
			}
		});
	}

	/**
	 * 弹出职称编辑窗口
	 * @param event
	 */
	private void onToTitleEditWindow(final AppEvent event) {
		final String id = event.getData();
		titleService.getTitle(id, new AsyncCallback<Title>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(Title result) {
				final AppEvent ae = new AppEvent(event.getType());
				ae.setData("titles", result);

				accountService.getAccountsByTitleID(id,
						new AsyncCallback<ArrayList<AccountSummaryDTO>>() {
							@Override
							public void onFailure(Throwable caught) {
								Dispatcher
										.forwardEvent(AppEvents.Error, caught);
							}

							@Override
							public void onSuccess(
									ArrayList<AccountSummaryDTO> result) {
								ae.setData("accounts", result);
								forwardToView(backendView, ae);
							}
						});
			}
		});
	}

	/**
	 * 执行跳转到添加新帐户窗口
	 */
	private void onToAddNewAccountWindow(final AppEvent event) {
		titleService.getDegrees(new AsyncCallback<ArrayList<Degree>>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(ArrayList<Degree> result) {
				final AppEvent ae = new AppEvent(event.getType());
				ae.setData("degrees", result);

				titleService.getTitles(new AsyncCallback<ArrayList<Title>>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(ArrayList<Title> result) {
						ae.setData("titles", result);
						forwardToView(backendView, ae);
					}
				});
			}
		});
	}

	/**
	 * 执行删除申请帐户操作
	 * @param event
	 */
	private void onDeleteNewAccount(AppEvent event) {
		final AccountDTO accountDTO = event.getData();
		accountService.deleteAccount(accountDTO.getId(),
				new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(Boolean result) {
						MessageBox.alert("提示信息", accountDTO.getFullName()
								+ "操作成功", null);
						Dispatcher
								.forwardEvent(AppEvents.ViewAccountManagement);
					}

				});
	}

	/**
	 * 执行开通新账户操作
	 * @param event
	 */
	private void onOpenedNewAccount(AppEvent event) {
		final AccountDTO accountDTO = event.getData();
		accountDTO.setSubmitDate(new Date());//开通时间
		accountDTO.setStatus("1");//设置状态为正常
		accountService.updateAccount(accountDTO,
				new AsyncCallback<AccountDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(AccountDTO result) {
						MessageBox.alert("提示信息", accountDTO.getFullName()
								+ "开通成功", null);
						Dispatcher
								.forwardEvent(AppEvents.ViewAccountManagement);
					}
				});
	}

	/**
	 * 跳转到开通新帐号页面
	 * @param event
	 */
	private void onToOpenedNewAccount(final AppEvent event) {
		AccountSummaryDTO account = event.getData();
		//人员
		accountService.getAccount(account.getId(),
				new AsyncCallback<AccountDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(AccountDTO result) {
						final AppEvent ae = new AppEvent(event.getType());
						ae.setData("accountDTO", result);
						//职级
						titleService.getDegrees(new AsyncCallback<ArrayList<Degree>>() {
							@Override
							public void onFailure(Throwable caught) {
								Dispatcher.forwardEvent(AppEvents.Error, caught);
							}

							@Override
							public void onSuccess(ArrayList<Degree> result) {	
								ae.setData("degrees", result);
								//职称
								titleService.getTitles(new AsyncCallback<ArrayList<Title>>() {
									@Override
									public void onFailure(Throwable caught) {
										Dispatcher.forwardEvent(AppEvents.Error, caught);
									}

									@Override
									public void onSuccess(ArrayList<Title> result) {
										ae.setData("titles", result);
										forwardToView(backendView, ae);
									}
								});
							}
						});
					}
				});
	}

	/**
	 * 弹出部门添加窗体
	 * @param event
	 */
	private void onDeptAddWindow(AppEvent event) {
		forwardToView(backendView, event);
	}

	/**
	 * 跳转到选择部门窗体
	 */
	private void onToDeptTreeSingleWindow(AppEvent event) {
		final AppEvent ae = new AppEvent(event.getType());
		ae.setData("pDept", event.getData());

		departmentService
				.getDepartmentTree(new AsyncCallback<DepartmentTreeDTO>() {

					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(DepartmentTreeDTO result) {
						ae.setData("root", result);
						forwardToView(backendView, ae);
					}

				});

	}

	/**
	 * 执行跳转到部门选择（可多选）窗口
	 * @param event
	 */
	private void onToDeptTreeMultiWindow(AppEvent event) {
		final AppEvent ae = new AppEvent(event.getType());
		ae.setData("dept", event.getData());
		departmentService
				.getDepartmentTree(new AsyncCallback<DepartmentTreeDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(DepartmentTreeDTO result) {
						ae.setData("root", result);
						forwardToView(backendView, ae);
					}
				});
	}

	/**
	 * 执行保存人员信息
	 * @param event
	 */
	private void onSaveAccount(AppEvent event) {
		final AccountDTO accountDTO = event.getData();
		accountDTO.setPassword(MD5.MD5Encode(accountDTO.getPassword()));
		accountDTO.setSubmitDate(new Date());
		accountService.updateAccount(accountDTO,
				new AsyncCallback<AccountDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(AccountDTO result) {
						//保存人员兼职信息
						createSaveMember(result);
						MessageBox.alert("提示信息", accountDTO.getFullName()
								+ "添加成功	", null);
						Dispatcher
								.forwardEvent(AppEvents.ViewDepartmentManagement);
					}
				});
	}

	/**
	 * 保存人员兼职信息
	 * @param result
	 */
	protected void createSaveMember(AccountDTO account) {
		String[] sArray = account.getByWorkDeptID().split(", ");
		for (String string : sArray) {
			Member member = new Member();
			member.setDeptID(string);
			member.setAccountID(account.getAccountID());
			member.setSortID(0);
			accountService.saveMember(member, new AsyncCallback<Void>() {
				@Override
				public void onFailure(Throwable caught) {
					Dispatcher.forwardEvent(AppEvents.Error, caught);
				}

				@Override
				public void onSuccess(Void result) {

				}
			});
		}
	}

	/**
	 * 添加部门
	 * @param event
	 */
	private void onSaveDepartment(AppEvent event) {
		final Department department = event.getData();
		departmentService.saveDepartment(department, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(Void result) {
				MessageBox.alert("提示信息", department.getDeptName() + "添加成功	",
						null);
				Dispatcher.forwardEvent(AppEvents.ViewDepartmentManagement);
			}
		});
	}

	/**
	 * 添加职称
	 * @param event
	 */
	private void onSaveTitle(AppEvent event) {
		final Title title = event.getData();
		titleService.addTitle(title, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(Void result) {
				MessageBox.alert("提示", title.getTitleCaption() + "添加成功	", null);
				Dispatcher.forwardEvent(AppEvents.ViewTitleManagement);
			}
		});
	}

	/**
	 * 浏览职称管理
	 * @param event
	 */
	private void onViewTitleManagement(final AppEvent event) {
		titleService.getTitles(new AsyncCallback<ArrayList<Title>>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(ArrayList<Title> result) {
				for (Title title : result) {
					title.updateProperties();
				}
				AppEvent ae = new AppEvent(event.getType(), result);
				forwardToView(backendView, ae);
			}
		});
	}

	/**
	 * 浏览会议管理
	 * @param event
	 */
	private void onViewMeetingManagement(AppEvent event) {
		final AppEvent ae = new AppEvent(event.getType());
		categoryService.getCategoryTree(new AsyncCallback<MeetingCategory>() {
			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.forwardEvent(AppEvents.Error, caught);
			}

			@Override
			public void onSuccess(MeetingCategory result) {
				ae.setData("root", result);
				forwardToView(backendView, ae);
			}
		});

	}

	/**
	 * 浏览部门管理主面板
	 * @param event
	 */
	private void onViewDepartmentManagement(AppEvent event) {
		final AppEvent ae = new AppEvent(event.getType());
		departmentService
				.getDepartmentTree(new AsyncCallback<DepartmentTreeDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Dispatcher.forwardEvent(AppEvents.Error, caught);
					}

					@Override
					public void onSuccess(DepartmentTreeDTO result) {
						ae.setData("root", result);
						forwardToView(backendView, ae);
					}
				});
	}

	/**
	 * 浏览人员管理主面板
	 * @param event
	 */
	private void onViewAccountManagement(AppEvent event) {
		forwardToView(backendView, event);
	}
	

}
